package com.qd.panda.service.third.fruit;

import cn.hutool.core.exceptions.ExceptionUtil;
import com.qd.common.panda.domain.entity.shop.order.ProductOrderDTO;
import com.qd.panda.model.ProductOrderSync;
import com.qd.panda.service.shop.product.ProductOrderSyncService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 处理同步到卖水果平台
 *
 * @author sjk
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class FruitService {
    private static final String URL = "";
    private final ProductOrderSyncService productOrderSyncService;
    @Async
    public void sync(List<ProductOrderDTO> list) {
        int state = 0;
        LocalDateTime now = LocalDateTime.now();
        for (ProductOrderDTO productOrderDTO : list) {
            ProductOrderSync productOrderSync = new ProductOrderSync();
            productOrderSync.setOrderId(productOrderDTO.getOrderId());
            productOrderSync.setUrl(URL);
            productOrderSync.setState(state);
            productOrderSync.setAddTime(now);
            try {
                //调用同步接口
            } catch (Exception e) {
                state = 1;
                productOrderSync.setErrMsg(ExceptionUtil.getMessage(e));
            }
            productOrderSyncService.save(productOrderSync);
            productOrderSync.freeData();
        }
        list.clear();
    }
}
